//
//  TieBaXiangQingTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/14.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//*******************帖子详情*******************
#import "TieBaXiangQingTableView.h"
#import "ReplyTopicModel.h"

@interface TieBaXiangQingTableView ()

@property(nonatomic,copy)TieZiModel *model;

@property(nonatomic,copy)UILabel *commentLab;

@end

@implementation TieBaXiangQingTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style TopicId:(NSInteger)topic_id
{
    if (self=[super initWithFrame:frame style:style]) {
        self.delegate=self;
        self.dataSource=self;
        _topic_id=topic_id;
        self.tableFooterView=[[UIView alloc]init];
        self.showsVerticalScrollIndicator=NO;
        _dataArray=[NSMutableArray array];
        self.tableHeaderView=[UIView new];
        self.tableFooterView=[UIView new];
        [self getHttpData];
        self.sendComment=^(NSString *content,NSString *replyId){
            if (replyId) {
                [self getCommentUser:content andReplyId:replyId];
                NSLog(@"不为空%@",replyId);
            }else{
                NSLog(@"为空%@",replyId);
                 [self getComment:content];
            }
            NSLog(@"11111======%@======%ld",content,_topic_id);
//            NSDictionary *dic=@{@"userHeadImage":@"community-backgroundImage.png",@"name":@"12345",@"content":content,@"userId":@"123456"};
           
        };
    }
    return self;
}
-(void)scrollToCellBottom
{
    
    NSUInteger sectionCount = [self numberOfSections];
    NSLog(@"tableView的组=========%ld",sectionCount);
    if (sectionCount) {
        [self setContentOffset:CGPointMake(0, self.contentSize.height -self.bounds.size.height) animated:YES];
    }
}
#pragma mark tableFooterView
-(UIView *)createTableFooterView
{
    UIView *footerView=[[UIView alloc]init];
    UITextField *answerTextField=[[UITextField alloc]initWithFrame:CGRectMake(15, 0, self.frame.size.width-15*2, 30)];
    answerTextField.placeholder=@"评论";
    answerTextField.font=[UIFont systemFontOfSize:15];
    answerTextField.backgroundColor=[UIColor whiteColor];
    [footerView addSubview:answerTextField];
    return footerView;
}


#pragma mark tableHeaderView
-(UIView *)createTableHeaderView
{
    UIView *headerView=[[UIView alloc]init];
    
    //用户头像
    UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 56, 56)];
    [headImageView sd_setImageWithURL:[NSURL URLWithString:_model.user_info[@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1.png"]];
    headImageView.layer.cornerRadius=56/2.0;
    headImageView.layer.masksToBounds=YES;
    [headerView addSubview:headImageView];
    
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.user_info[@"avatar_url"]]];
    UIImage *myHeadImage=[UIImage imageWithData:data];
    headImageView.image=myHeadImage;
    
    
    
    
    
    //盖按钮
    UIButton *headImageBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
    headImageBtn1.frame=headImageView.frame;
   // headImageBtn1.tag=[_arr[0][@"userId"] integerValue];
    [headImageBtn1 addTarget:self action:@selector(headImageBtnClick1:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:headImageBtn1];
    NSString *userId=_model.userId;
    headImageBtn1.tag=[userId integerValue];
    
    
    CGFloat height=(56-8)/3.0;
    //用户名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 20, 80, height)];
    name.text=_model.user_info[@"nikename"];
    name.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    name.font=[UIFont systemFontOfSize:13];
    name.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 20, [self calculateWidthWithLable:name], height);
    [headerView addSubview:name];
    
    //性别
    UIImageView *sexImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(name.frame)+2, 20, height, height)];
    [headerView addSubview:sexImageView];
    NSInteger sexState;
    if (_model.user_info[@"sex"]) {
        sexState=[_model.user_info[@"sex"] integerValue];
    }else{
        sexState=1;
    }
    if (sexState==0) {
        sexImageView.image=[UIImage imageNamed:@"sex_man"];
    }else{
        sexImageView.image=[UIImage imageNamed:@"sex_woman"];
    }
    
    //标签
    UILabel *biaoqian=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame)+4, 40, height)];
    biaoqian.text=_model.tag_name;
    biaoqian.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    biaoqian.font=[UIFont systemFontOfSize:12];
    biaoqian.layer.borderColor=[UIColor colorWithRed:106/255.0 green:198/255.0 blue:223/255.0 alpha:1].CGColor;
    biaoqian.layer.borderWidth=1.0;
    biaoqian.layer.cornerRadius=2.0;
    biaoqian.textAlignment=NSTextAlignmentCenter;
    biaoqian.frame=CGRectMake(biaoqian.frame.origin.x, biaoqian.frame.origin.y, [self calculateWidthWithLable:biaoqian]+8, biaoqian.frame.size.height);
    [headerView addSubview:biaoqian];
    
    //时间
    UILabel *time=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(biaoqian.frame)+4, 60, height)];
    time.text=_model.created_at;
    time.textColor=[UIColor colorWithRed:87/255.0 green:194/255.0 blue:209/255.0 alpha:1];
    time.font=[UIFont systemFontOfSize:13];
     time.frame=CGRectMake(time.frame.origin.x, time.frame.origin.y, [self calculateWidthWithLable:time], time.frame.size.height);
    [headerView addSubview:time];
    
    
    
   // CGFloat height2=56/3.0;
    
    //评论
    UIImageView *commentImageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width-10-30-20-30-20-1, 20, 20, 20)];
    commentImageView.image=[UIImage imageNamed:@"shequ_comment"];
    [headerView addSubview:commentImageView];
    
    
    
    UILabel *commentLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(commentImageView.frame)+1, 20, 30, 20)];
    commentLab.text=_model.comment_num;
    
    commentLab.font=[UIFont systemFontOfSize:10];
    commentLab.textColor=[UIColor grayColor];
    [headerView addSubview:commentLab];
    _commentLab=commentLab;
    
    
    
    //点赞
    UIImageView *zanImageView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(commentLab.frame), 20, 20, 20)];
    zanImageView.image=[UIImage imageNamed:@"shequ_dianzan"];
    [headerView addSubview:zanImageView];
    NSInteger zanState=[_model.is_like integerValue];
    zanImageView.tag=zanState;
    if (zanState==0) {
        zanImageView.image=[UIImage imageNamed:@"shequ_dianzan_no.png"];
    }else{
        zanImageView.image=[UIImage imageNamed:@"shequ_dianzan_yes.png"];
    }
    //添加手势
    zanImageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHappen1:)];
    [zanImageView addGestureRecognizer:tap1];

    
    UILabel *zanLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(zanImageView.frame)+1, 20, 30, 20)];
    zanLab.text=_model.like_num;
    zanLab.font=[UIFont systemFontOfSize:10];
    zanLab.textColor=[UIColor grayColor];
    [headerView addSubview:zanLab];
    self.dianzan=^(NSString *number){
        zanLab.text=_model.like_num;
    };
    
    //精华帖标识
    UIImageView *jinghuatie=[[UIImageView alloc]initWithFrame:CGRectMake(commentImageView.frame.origin.x, CGRectGetMaxY(commentImageView.frame)+3, 20+30+2+20+30, 34*(102/168.0))];
    jinghuatie.image=[UIImage imageNamed:@"tieziImage_01"];
    if ([_model.is_popular integerValue]>0) {
        jinghuatie.image=[UIImage imageNamed:@"tieziImage_02"];
    }
    if ([_model.is_great integerValue]>0){
        jinghuatie.image=[UIImage imageNamed:@"tieziImage_03"];
    }
    [headerView addSubview:jinghuatie];
    
    
    //帖子标题
    UILabel *topicLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(headImageView.frame)+5, self.frame.size.width-10-10, 100-10-50)];
    topicLab.text=_model.title;
    topicLab.textColor=[UIColor blackColor];
    topicLab.font=[UIFont systemFontOfSize:15];
    topicLab.numberOfLines=0;
    CGSize topicLabSize=[topicLab boundingRectWithSize:CGSizeMake(topicLab.frame.size.width, 0)];
    topicLab.frame=CGRectMake(10, CGRectGetMaxY(headImageView.frame)+5, self.frame.size.width-10-10, topicLabSize.height);
    [headerView addSubview:topicLab];
    
    
    //帖子内容
    UILabel *contentLab=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(topicLab.frame)+5, self.frame.size.width-10-10, 112-10-50-40-1)];
    contentLab.text=_model.content;
    contentLab.textColor=[UIColor blackColor];
    contentLab.font=[UIFont systemFontOfSize:13];
    contentLab.numberOfLines=0;
    CGSize contentLabsize=[contentLab boundingRectWithSize:CGSizeMake(contentLab.frame.size.width, 0)];
    contentLab.frame=CGRectMake(10, CGRectGetMaxY(topicLab.frame)+5, self.frame.size.width-10-10, contentLabsize.height);
    [headerView addSubview:contentLab];
    
    //发表的图片
    //发表的图片
    CGFloat img_w=(self.frame.size.width-10*6)/5.0;
    CGFloat img_h=img_w*1.1;
    NSInteger hang=[self calculatingHang:_model.media_urls];
    
    for (int i=0; i<hang; i++) {
        for (int j=0; j<5; j++) {
            if (i*5+j<[_model.media_urls count]) {
                UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10+(img_w+10)*j, CGRectGetMaxY(contentLab.frame)+10+(img_h+5)*i, img_w, img_h)];
                [imgView sd_setImageWithURL:[NSURL URLWithString:_model.media_urls[i*5+j]] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
               //NSLog(@"图片的地址==========%@",_model.media_urls[i*5+j]);
                imgView.tag=i*5+j;
                [headerView addSubview:imgView];
                
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTapHappen:)];
                imgView.userInteractionEnabled=YES;
                [imgView addGestureRecognizer:tap];
            }
        }
    }

    if ([_model.media_urls count]>0) {
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h+9, self.frame.size.width, 1)];
        line.backgroundColor=[UIColor grayColor];
        if (hang==1) {
            line.frame=CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h+9, self.frame.size.width, 1);
        }else if (hang==2){
            line.frame=CGRectMake(0, CGRectGetMaxY(contentLab.frame)+10+img_h*2+5+9, self.frame.size.width, 1);
        }
        [headerView addSubview:line];
        headerView.frame=CGRectMake(0, 0, self.frame.size.width, CGRectGetMaxY(line.frame));
    }else{
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(contentLab.frame)+9, self.frame.size.width, 1)];
        line.backgroundColor=[UIColor grayColor];
        [headerView addSubview:line];
        headerView.frame=CGRectMake(0, 0, self.frame.size.width, CGRectGetMaxY(line.frame));
    }
    return headerView;
}


//根据文字计算宽度和高度
-(CGFloat)calculateWidthWithLable:(UILabel *)label
{
    CGSize size=[label.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil]];
    return size.width;
}
//tableView的代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    ReplyTopicModel *model=_dataArray[indexPath.row];
    
    
    
    //用户头像
    UIImageView *headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 56, 56)];
    //headImageView.image=[UIImage imageNamed:_dataArray[indexPath.row][@"user_info"][@"avatar_url"]];
    [headImageView sd_setImageWithURL:[NSURL URLWithString:model.user_info[@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"155x155-1"]];
    headImageView.layer.cornerRadius=56/2.0;
    headImageView.layer.masksToBounds=YES;
    [cell.contentView addSubview:headImageView];
    //盖按钮
    UIButton *headImageBtn1=[UIButton buttonWithType:UIButtonTypeCustom];
    headImageBtn1.frame=headImageView.frame;
    headImageBtn1.tag=indexPath.row+100000;
    [headImageBtn1 addTarget:self action:@selector(headImageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:headImageBtn1];
    
    
    CGFloat height=56/3.0;
    //用户名
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 10, 80, height)];
    name.text=model.user_info[@"nikename"];
    name.textColor=[UIColor grayColor];
    name.font=[UIFont systemFontOfSize:13];
    name.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, 10, [self calculateWidthWithLable:name], height);
    [cell.contentView addSubview:name];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *userId=[user objectForKey:@"userId"];
    
    NSInteger currentUserId=[userId integerValue];
    NSInteger commentUserId=[model.user_id integerValue];
    
    
    if (currentUserId==commentUserId) {
        //删除评论
        UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        deleteBtn.frame=CGRectMake(CGRectGetMaxX(name.frame)+5, name.frame.origin.y, 32, name.frame.size.height);
        deleteBtn.titleLabel.font=[UIFont systemFontOfSize:13];
        deleteBtn.tag=indexPath.row;
        [deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [deleteBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:deleteBtn];
    }
    
    
    
    
    
    //回复的内容
    UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame)+4, self.frame.size.width-(CGRectGetMaxX(headImageView.frame)+8)-10, height)];
    content.text=model.content;
    content.textColor=[UIColor blackColor];
    content.numberOfLines=0;
    content.font=[UIFont systemFontOfSize:13];
    CGSize contentSize=CGSizeMake(content.frame.size.width, 0);
    content.frame=CGRectMake(CGRectGetMaxX(headImageView.frame)+8, CGRectGetMaxY(name.frame)+4, self.frame.size.width-(CGRectGetMaxX(headImageView.frame)+8)-10, [content boundingRectWithSize:contentSize].height);
    [cell.contentView addSubview:content];
    
    return cell;
}


//点击发表的图片
-(void)imgTapHappen:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    NSLog(@"手势响应了");
    //[self.imgDelegate bigImage:imgView.image];
    [self.imgDelegate bigImage:imgView.tag AndImageArrays:_model.media_urls];
}





//删除评论
-(void)deleteBtnClick:(UIButton *)sender
{
    ReplyTopicModel *model=_dataArray[sender.tag];
    [_dataArray removeObjectAtIndex:sender.tag];
    //self deleteRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>
    [self reloadData];
    
    
    NSLog(@"tag====%ldid====%@",sender.tag,model.myId);
    [self getDeleteCommentData:model.myId];
}


//计算评论 点赞的个数
-(NSString *)getLabel:(NSString *)number
{
    NSInteger num=[number floatValue];
    if (num>10000) {
        number=[NSString stringWithFormat:@"%.1f万",num/10000.0];
    }
    return number;
    
}
//每个cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReplyTopicModel *model=_dataArray[indexPath.row];
    
    //回复的内容
    UILabel *content1=[[UILabel alloc]initWithFrame:CGRectMake(56+10+2, 10+56/3.0, self.frame.size.width-60-10-2, 0)];
    content1.text=model.content;
    content1.textColor=[UIColor blackColor];
    content1.numberOfLines=0;
    content1.font=[UIFont systemFontOfSize:13];
    CGSize contentSize=CGSizeMake(content1.frame.size.width, 0);
     NSLog(@"222222222======%@",model.content);
    NSLog(@"发表内容的高度=======%f",[content1 boundingRectWithSize:contentSize].height);
    if ([content1 boundingRectWithSize:contentSize].height<=56-56/3.0) {
        return 60+5+10+4;
    }else{
        return 10+56/3.0+[content1 boundingRectWithSize:contentSize].height+5+10+4;
    }
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReplyTopicModel *model=_dataArray[indexPath.row];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSInteger userId=[[user objectForKey:@"userId"]integerValue];
    NSInteger currentUserId=[model.user_id integerValue];
    if (userId == currentUserId) {
        return;
    }
    [self.imgDelegate startPostComment:model.user_info[@"nikename"] ReplyId:model.myId];
}

//head中用户头像点击
-(void)headBtnClick:(UIButton *)sender
{
    NSLog(@"头像");
    [self.imgDelegate gotoMyAttentionViewController:[NSString stringWithFormat:@"%ld",sender.tag]];
}

//头像按钮的响应
-(void)headImageBtnClick1:(UIButton *)sender
{
    NSLog(@"头像========%ld",sender.tag);
    [self.imgDelegate gotoMyAttentionViewController:[NSString stringWithFormat:@"%ld",sender.tag]];
}

//头像按钮的响应
-(void)headImageBtnClick:(UIButton *)sender
{
    NSLog(@"头像");
    ReplyTopicModel *model=_dataArray[sender.tag-100000];
    [self.imgDelegate gotoMyAttentionViewController:model.user_id];
}

//点赞
-(void)zanImageBtnClick:(UIButton *)sender
{
    NSLog(@"点赞");
}

//发表的图片
-(void)imgBtnClick:(UIButton *)sender
{
    NSLog(@"图片响应");
    //[self.imgDelegate bigImage:sender.imageView.image];
}


//计算行
-(NSInteger)calculatingHang:(NSArray *)arr
{
    NSInteger hang=0;
    if (arr.count>5) {
        hang=2;
    }else if(arr.count==0){
        hang=0;
    }else{
        hang=1;
    }
    return hang;
}
//点赞
-(void)tapHappen1:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    NSLog(@"=========%ld",imgView.tag);
    NSInteger zanState=imgView.tag;
    CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    k.values = @[@(0.1),@(1.0),@(1.5)];
    k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    k.calculationMode = kCAAnimationLinear;
    if (zanState==0) {
        //点赞
        imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_yes"].CGImage;
        imgView.tag=1;
        NSLog(@"点赞");
        NSInteger zanNumber=[_model.like_num integerValue]+1;
        _model.like_num=[NSString stringWithFormat:@"%ld",zanNumber];
        self.dianzan(nil);
        [self getZanHttpData:_model.topic_id];
    }else{
        //取消赞
        imgView.layer.contents=(id)[UIImage imageNamed:@"shequ_dianzan_no"].CGImage;
        imgView.tag=0;
        NSLog(@"取消赞");
        NSInteger zanNumber=[_model.like_num integerValue]-1;
        _model.like_num=[NSString stringWithFormat:@"%ld",zanNumber];
        self.dianzan(nil);
        [self getCancelZanHttpData:_model.topic_id];
        zanState=0;
    }
    [imgView.layer addAnimation:k forKey:@"SHOW"];
    //TieBaTableView
    if ([_fromWhere isEqualToString:@"tieba"]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadTieBaTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    }
    //SelfTableView
    if (_centerTag==1) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadSelfTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    }
    
    //TieZiTableView
    if (_centerTag==2) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadTieZiTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    }
    //CareTableView
    if (_centerTag==3) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadCareTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    }
    if (_centerTag==4) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"ReloadHotAndGreatTableView" object:nil userInfo:@{@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"is_like":[NSString stringWithFormat:@"%ld",zanState],@"like_num":_model.like_num}];
    }
}


-(void)getHttpData
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    // [self isLoading:YES];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    //[self showCicle:YES];
    NSLog(@"qqqqqqq========%ld",_topic_id);
    [manager GET:[NSString stringWithFormat:TieZiDetail,_topic_id,token] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // [self isLoading:NO];  
        //[self showCicle:NO];
        [self.header endRefreshing];
        //[self.footer endRefreshing];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
         NSLog(@"%@",dic);
        
        if (dic) {
            
            UIImage *image=[[UIImage alloc]init];
            if ([dic[@"media_urls"] count]<=0) {
                
            }else{
                NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:dic[@"media_urls"][0]]];
                image=[UIImage imageWithData:data];
            }

            //UIImage *img=[UIImage ima]
            
            self.backTopicData(dic[@"title"],dic[@"content"],image);
            
            _model=[[TieZiModel alloc]initWithDictionary:dic];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"ShareContent" object:nil userInfo:@{@"title":_model.title,@"content":_model.content}];
            if (dic[@"replies"]) {
                _dataArray=[NSMutableArray array];
                for (NSDictionary *dic2 in dic[@"replies"]) {
                    ReplyTopicModel *model=[[ReplyTopicModel alloc]initWithDictionary:dic2];
                    [_dataArray addObject:model];
                }
            }
            
            
            

            dispatch_async(dispatch_get_main_queue(), ^{
                self.tableHeaderView=nil;
                self.tableHeaderView= [self createTableHeaderView];
                [self reloadData];
            });
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        // [self isLoading:NO];
        [self showCicle:NO];
        [self.header endRefreshing];
        //[self.footer endRefreshing];
    }];

}


//获取数据  下拉刷新
-(void)getHttpDataRefresh
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];

    [manager GET:BiaoTi parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        // NSLog(@"%@",dic);
        [self.header endRefreshing];
        [self.footer endRefreshing];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.tableHeaderView=nil;
            self.tableHeaderView= [self createTableHeaderView];
            [self reloadData];
        });
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        [self showCicle:NO];
        [self.header endRefreshing];
        [self.footer endRefreshing];
    }];
}


///帖子点赞
-(void)getZanHttpData:(NSString *)tieziId
{
    NSLog(@"帖子的id======%@",tieziId);
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager POST:[NSString stringWithFormat:@"%@?token=%@&topic_id=%@",TieZiIsLike,token,tieziId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"tieziZanData===============%@点赞结果========%@",dic,dic[@"status"][@"msg"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
//取消点赞
-(void)getCancelZanHttpData:(NSString *)tieziId
{
    NSLog(@"帖子的id======%@",tieziId);
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    
    [manager DELETE:[NSString stringWithFormat:@"%@?token=%@&topic_id=%@",TieZiCancellike,token,tieziId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"tieziCancelZanData===============%@=====取消赞结果======%@",dic,dic[@"status"][@"msg"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}




//获取评论的数据源   直接评论帖子
-(void)getComment:(NSString *)content
{
//    NSInteger comment_num=[_commentLab.text integerValue];
//    comment_num+=1;
//    _commentLab.text=[NSString stringWithFormat:@"%ld",comment_num];
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    content=[content stringByReplacingOccurrencesOfString:@" " withString:@""];
    //@"http://192.168.11.115/api/community/reply?token=%@&content=%@&topic_id=%ld"
    NSDictionary *params=@{@"content":content,@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id]};
    
    
   // NSDictionary *params=@{@"token":token,@"title":@"",@"content":content,@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id]};
    //@"http://192.168.11.115/api/community/reply?token=%@&title=test&content=%@&topic_id=%ld"
    [manager POST:[NSString stringWithFormat:CommentTopic,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"commentTopic===============%@",dic);
        [self getHttpData];
        //[self scrollToCellBottom];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];

}
//评论评论的人
-(void)getCommentUser:(NSString *)content andReplyId:(NSString *)replyId
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    content=[content stringByReplacingOccurrencesOfString:@" " withString:@""];
    // NSDictionary *params=@{@"token":token,@"title":@"",@"content":content,@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id]};
    NSDictionary *params=@{@"content":content,@"topic_id":[NSString stringWithFormat:@"%ld",_topic_id],@"parent_reply_id":replyId};
    
    //@"http://192.168.11.115/api/community/reply?token=%@&title=test&content=%@&topic_id=%ld"
    [manager POST:[NSString stringWithFormat:CommentTopic,token] parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"commentTopic===============%@",dic);
        [self getHttpData];
        //[self scrollToCellBottom];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}




//删除评论
-(void)getDeleteCommentData:(NSString *)replyId
{
    NSInteger comment_num=[_commentLab.text integerValue];
    comment_num-=1;
    _commentLab.text=[NSString stringWithFormat:@"%ld",comment_num];
    
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    NSString *token=[user objectForKey:@"token"];
    
    [manager DELETE:[NSString stringWithFormat:DeleteComment,token,replyId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"删除评论%@",dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error====%@",error.localizedDescription);
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
